<template>
  <ele-modal
    width="460px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    title="编辑文件夹"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="102px">
      <el-form-item label="文件夹名称:" prop="name">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入文件夹名称"
        />
      </el-form-item>
      <el-form-item label="文件夹类型:" prop="type">
        <el-radio-group>
          <!--1:视频2：音频3：台本4：图片-->
          <el-select class="ele-block v-width" v-model="form.type" :disabled="form.type ? true : false">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="公共文件夹:" prop="isCommon">
        <el-radio-group v-model="form.isCommon">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { updateUserFile } from '@/api/creatCenter/material';

  export default {
    props: {
      // 父级 id
      parentId: Number,
      // 弹窗是否打开
      visible: Boolean,
      // 文件数据
      data: Object
    },
    data() {
      return {
        options: [
          {
            value: '0',
            label: '文件夹'
          },
          {
            value: '1',
            label: '视频'
          }, {
            value: '2',
          label: '音频'
        }, {
          value: '3',
          label: '台本'
        }, {
          value: '4',
          label: '图片'
        }],
        // 表单数据
        form: {
          name: '',
          type: '',
          isCommon: 0,
        },
        // 表单验证规则
        rules: {
          name: [
            {
              required: true,
              message: '请输入文件/夹名称',
              trigger: 'blur'
            }
          ], type: [
            {
              required: true,
              message: '请输入文件夹类型',
              trigger: 'blur'
            }
          ], isCommon: [
            {
              required: true,
              message: '请选择是否公共文件夹',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false
      };
    },
    created() {
      console.log("form==========",this.form,'data',this.data)
      console.log("this.")
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          updateUserFile({
            //当前文件夹id
            id:this.data.id,
            username: this.$store.state.user.info.username,
            //父级id
            parentId: this.parentId,
            type: this.form.type,
            name: this.form.name,
            //是否公共文件夹 0否，1是
            isCommon: this.form.isCommon,
          })
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e);
            });
        });
      },
      /* 更新 visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (!visible) {
          this.$refs.form.clearValidate();
          this.form.name = '';
        } else if (this.data) {
          this.form.name = this.data.name;
          this.form.type = (this.data.type).toString();  //数组格式为字符串，传入为数字，则需转换
          this.form.isCommon = this.data.isCommon;
        }
      }
    }
  };
</script>
<style>
  .v-width{
    width: 318px;
  }
</style>
